package com.dsjy.yf.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.YardDO;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.mapper.YardMapper;
import com.dsjy.yf.address.service.SubareaService;
import com.dsjy.yf.address.service.YardRoadService;
import com.dsjy.yf.address.service.YardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-20 9:09
 */
@Service
public class YardServiceImpl implements YardService {
    @Resource
    private YardMapper yardMapper;
    @Autowired
    private SubareaService subareaService;
    @Autowired
    private YardRoadService yardRoadService;


    @Override
    public YardDO searchYardWithId(Integer id) {
        return yardMapper.searchYardWithId(id);
    }


    @Override
    @Cacheable(value = "yard", key = "'searchYardWithCode'+#parentCode+#flag")
    public List<YardDO> searchYardWithCode(Integer parentCode, Boolean flag) {
        return yardMapper.searchYardWithCode(parentCode, flag);
    }


    @Override
    @Caching(evict = {
            @CacheEvict(value = "yard", condition = "#yardDO.parentCode != null", key = "'searchYardWithCode'+#yardDO.parentCode+'false'"),
            @CacheEvict(value = "yard", condition = "#yardDO.parentCode != null", key = "'searchYardWithCode'+#yardDO.parentCode+'true'"),
    })
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer addUpdDel(String operate, YardDO yardDO) {
        Integer result = null;
        if (OperateEnum.ADD.getOperate().equals(operate) && yardDO.getYardName() != null) {
            result = addYard(yardDO);
        } else if (OperateEnum.UPDATE.getOperate().equals(operate) && yardDO.getId() != null) {
            result = updateYard(yardDO);
        }
        return result;
    }

    @Override
    public Integer updateYard(YardDO yardDO) {
        return yardMapper.updateYard(yardDO);
    }

    @Override
    public Integer addYard(YardDO yardDO) {
        if (yardDO.getParentCode() == null || String.valueOf(yardDO.getParentCode()).length() != 6) {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
        yardDO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return yardMapper.addYard(yardDO);
    }

    @Override
    public Integer delYard(Integer id) {
        subareaService.delSubareaByParentCode(id);
        yardRoadService.delYardRoadByParentCode(id);
        return yardMapper.delYard(id);
    }

    @Override
    public IPage<YardDO> searchWithPage(Integer pageNum, Integer pageSize, QueryWrapper wrapper) {
        wrapper.setEntityClass(YardDO.class);
        Page<YardDO> page = new Page<>(pageNum, pageSize);
        return yardMapper.selectPage(page, wrapper);
    }
}
